/**
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 */

import { createModel } from "@rematch/core";
import { IRootState } from "../../store";
import {
  getSearchLogList,
  delPseudo,
  getBatchAndName,
  deleteById
} from "src/pages/SearchLog/components/searchLogManage/SearchLogAPI";
import { message } from "antd";

export interface ISearchLog {
  id: string;
  ruleId: string;
  goodsName: string;
  goodsBatch: string;
  securityCodeImg: string;
  securityCode: string;
  queryNumber: number;
  queryArea: string;
}

interface IState {
  searchLogList: ISearchLog[];
  pageTotal: number;
  batchAndNameList: ISearchLog[];
}

const initalState: IState = {
  searchLogList: [],
  pageTotal: 0,
  batchAndNameList: []
};

export interface ITagList {
  id: string;
  name: string;
}

export const searchLog = createModel({
  state: initalState,
  reducers: {
    setSearchLogList: (state: IState, payload): IState => {
      return { ...state, searchLogList: payload };
    },
    setBatchAndNameList: (state: IState, payload): IState => {
      return { ...state, batchAndNameList: payload };
    }
  },
  effects: dispatch => ({
    async getSearchLogList(payload: any, rootState: IRootState) {
      const res = await getSearchLogList(payload);
      if (res.status !== 200) {
        return;
      }
      initalState.pageTotal = res.data.totalElements;
      await dispatch.searchLog.setSearchLogList(res.data.content);
    },
    async delPseudo(payload: any, rootStat: IRootState) {
      const res = await delPseudo(payload);
      if (res.status !== 200) {
        return;
      }
      message.info("删除成功!");
    },
    async getBatchAndName(payload: any, rootState: IRootState) {
      const res = await getBatchAndName(payload);
      if (res.status !== 200) {
        return;
      }
      await dispatch.searchLog.setBatchAndNameList(res.data);
    },
    async deleteById(id: string, rootStat: IRootState) {
      const res = await deleteById(id);
      if (res.status !== 200) {
        return;
      }
      message.success("删除成功!");
      dispatch.searchLog.getSearchLogList({
        page: 0,
        goodsName: "",
        goodsBatch: ""
      });
    }
  })
});
